const enterpriseVue = new Vue({
  el: "#app",
  data() {
    return {
      pickerOptions: pickerOptions,
      rules: {},
      form: {},
      menuList: [],
      UserNumber: "",
      allCheckedKeys: [],
      loading: false,
    };
  },
  mounted() {
    const User_Number = sessionStorage.getItem("User_Number");
    this.User_Number = User_Number;
    this.getData();
    this.setCheckMenu(User_Number);
  },
  methods: {
    submitForm() {
      let _this = this;
      _this.loading = true;
      $.ajax({
        url: DomainName + "/RoleEditAction?token=" + $.cookie("token"),
        type: "POST",
        data: {
          Data: JSON.stringify(_this.allCheckedKeys),
          UserNumber: _this.User_Number,
        },
        success: function (res) {
          if (res.Status === "success") {
            _this.$message.success("提交成功");
            window.history.go(-1);
          } else {
            _this.$message.error("提交失败，请稍后再试");
          }
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试");
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false;
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href =
              absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
      });
    },
    getData() {
      let _this = this;
      $.ajax({
        type: "GET",
        url: `${DomainName}/SystemMenu?token=${$.cookie("token")}&system=${
          JSON.parse($.cookie("system")).System_SerialNumber
        }`,
        complete: function (XMLHttpRequest) {
          _this.loading = false;
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href =
              absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Result) {
          _this.menuList = Result; //将获取的数据存到表格数据中
        },
        dataType: "json",
      });
    },
    handleCheckChange() {
      let halfKeys = this.$refs.tree.getHalfCheckedKeys();
      let keys = this.$refs.tree.getCheckedKeys();
      this.allCheckedKeys = [...keys, ...halfKeys];
    },
    setCheckMenu(UserNumber) {
      let _this = this;
      $.ajax({
        type: "POST",
        url: DomainName + "/RoleAction?token=" + $.cookie("token"),
        data: {
          UserNumber,
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false;
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href =
              absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Result) {
          console.log(Result);
          _this.handleCheckChange();
          _this.$refs.tree.setCheckedKeys(Result, true);
        },
        dataType: "json",
      });
    },
  },
});
